<?php
include("template_top.php");
if(isset($_POST['title'])){
	if(isset($_POST['update'])){
		$sql = "UPDATE `tbAnnouncements` SET `sTitle` = :title, `sAnnouncement` = :announcement, `sExpireDate` = :date, `sExpireTime` = :time WHERE `iID` = :id";
		db::Query($sql, array(':title'=>$_POST['title'], ':announcement'=>$_POST['announcement'], ':date'=>$_POST['date'], ':time'=>$_POST['time'], ':id'=>$_POST['update']));
		$_GET['msg'] = "Announcement Updated Successfully";
		$_GET['id'] = "";
		$_GET['mode'] = "";
	}else{
		$sql = "INSERT INTO `tbAnnouncements` VALUES(null,:date,:time,:title,:announcement,:name,:curdate,:curtime)";
		db::Query($sql, array(':date'=>$_POST['date'], ':time'=>$_POST['time'], ':title'=>$_POST['title'], ':announcement'=>$_POST['announcement'], ':name'=>$USER['sFname']." ".$USER['sLname'], ':curdate'=>getCurrentDate(), ':curtime'=>getCurrentTime()));
		$_GET['msg'] = "Announcement Added Successfully";
	}
}
if(@$_GET['mode'] == 'delete'){
	$sql = "DELETE FROM `tbAnnouncements` WHERE `iID` = :id";
	db::Query($sql, array(':id'=>$_GET['id']));
	$_GET['msg'] = "Announcement Deleted Successfully";
	$_GET['id'] = "";
	$_GET['mode'] = "";
}
if(@$_GET['mode'] == 'edit'){
	$sql = "SELECT * FROM `tbAnnouncements` WHERE `iID` = :id";
	$q = db::Query($sql, array(':id'=>$_GET['id']));
	$row = $q->fetch();
}
?>
<script>
  $(document).ready(function() {
	$("#datepicker").datepicker({ dateFormat: 'm/d/yy' });
	$('#timepicker').timepickr({convention:12, format12: "{h:2.d}:{m:02.d} {suffix:s}"});
  })
</script>
<h1>Announcement Control Center</h1>
<p>
<table width="100%">
	<tr>
    	<td width="50%">
        	<h4><?php if(@$_GET['mode'] == 'edit'){ ?>Edit<?php }else{ ?>Add A New<?php } ?> Announcement</h4>
            <form action="" method="POST">
            <table width="100%">
            	<tr>
                	<td width="100" align="right">
                    	Title:
                    </td>
                    <td>
                    	<input type="text" name="title" style="width:97%" value="<?php echo $row['sTitle'] ?>" />
                    </td>
                 </tr>
            	<tr>
                	<td width="100" align="right">
                    	Announcement:
                    </td>
                    <td>
                    	<textarea name="announcement" style="width:97%" rows="7"><?php echo $row['sAnnouncement'] ?></textarea>
                    </td>
                 </tr>
            	<tr>
                	<td width="100" align="right">
                    	Expiration Date:
                    </td>
                    <td>
                    	<input type="text" name="date" id="datepicker" style="width:97%" value="<?php echo $row['sExpireDate'] ?>" />
                    </td>
                 </tr>
            	<tr>
                	<td width="100" align="right">
                    	Expiration Time:
                    </td>
                    <td>
                    	<input type="text" name="time" id="timepicker" style="width:97%" value="<?php echo $row['sExpireTime'] ?>" />
                    </td>
                 </tr>
            	<tr>
                	<td width="100" align="right">
                    	<?php if(@$_GET['mode'] == 'edit'){ ?>
                        <input type="hidden" name="update" value="<?php echo $row['iID'] ?>" />
                        <?php } ?>
                    </td>
                    <td align="right">
                    	<input type="submit" value="<?php if(@$_GET['mode'] == 'edit'){ ?>Edit<?php }else{ ?>Add<?php } ?> Announcement" />
                    </td>
                 </tr>
             </table>
             </form>
        </td>
    </tr>
</table>
</p>
<h4>Current Announcements</h4>
<p>
<?php
$sql = "SELECT * FROM `tbAnnouncements` ORDER BY `iID` ASC";
$q = db::Query($sql);
$c = 1;
if($c ==0){
?>
<center>
<h1>There are currently no announcements</h1>
</center>
<?php
}else{
?>
<table width="100%">
<tr>
	<td style="border-bottom:solid 1px #000000; font-weight:bold">
    	Title
    </td>
    <td style="border-bottom:solid 1px #000000; font-weight:bold">
    	Submission User
    </td>
    <td style="border-bottom:solid 1px #000000; font-weight:bold">
    	Expires
    </td>
    <td style="border-bottom:solid 1px #000000; font-weight:bold">
    	Date/Time
    </td>
    <td style="border-bottom:solid 1px #000000; font-weight:bold">
    	Options
    </td>
</tr>
<?php
while($row = $q->fetch()){
?>
<tr>
	<td>
    	<?php echo $row['sTitle'] ?>
    </td>
    <td>
    	<?php echo $row['sSubmit'] ?>
    </td>
    <td>
    	<?php echo $row['sExpireDate']." @ ".$row['sExpireTime'] ?>
    </td>
    <td>
    	<?php echo $row['sDate']." @ ".$row['sTime'] ?>
    </td>
    <td>
    	<a href="?id=<?php echo $row['iID'] ?>&mode=edit">Edit</a> | <a href="?id=<?php echo $row['iID'] ?>&mode=delete">Delete</a>
    </td>
</tr>
<?php
}
?>
</table>
<?php } ?>
</p>
<?php
include("template_bot.php");
?>